﻿using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Layouts;
using CCTool.Scripts.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Button = ArcGIS.Desktop.Framework.Contracts.Button;
using MessageBox = ArcGIS.Desktop.Framework.Dialogs.MessageBox;
using System.Windows.Forms;
using ArcGIS.Desktop.Mapping;
using Microsoft.Office.Core;
using ArcGIS.Core.Data.DDL;
using FieldDescription = ArcGIS.Core.Data.DDL.FieldDescription;
using Row = ArcGIS.Core.Data.Row;
using ArcGIS.Desktop.Editing.Attributes;
using System.Security.Cryptography;
using ArcGIS.Desktop.Editing.Templates;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using System.Net;
using System.Net.Http;
using System.Windows.Media;
using System.Windows.Shapes;
using Path = System.IO.Path;
using ArcGIS.Desktop.Internal.Catalog.Wizards;
using ArcGIS.Desktop.Internal.Layouts.Utilities;
using System.Windows.Documents;
using ActiproSoftware.Windows;
using System.Windows;
using System.Runtime.InteropServices;
using ArcGIS.Desktop.Internal.Mapping.Locate;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.GeoProcessing;
using Geometry = ArcGIS.Core.Geometry.Geometry;
using Polygon = ArcGIS.Core.Geometry.Polygon;
using ArcGIS.Core.Data.Exceptions;
using Table = ArcGIS.Core.Data.Table;
using SpatialReference = ArcGIS.Core.Geometry.SpatialReference;
using Brushes = System.Windows.Media.Brushes;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using CCTool.Scripts.ToolManagers;
using NPOI.SS.Util;
using NPOI.SS.Formula.Functions;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using Aspose.Cells.Drawing;
using ArcGIS.Core.Data.UtilityNetwork.Trace;
using MathNet.Numerics.LinearAlgebra.Factorization;
using NPOI.OpenXmlFormats.Spreadsheet;
using NPOI.Util;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Math.EC;
using ArcGIS.Core.Internal.CIM;
using Polyline = ArcGIS.Core.Geometry.Polyline;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static Org.BouncyCastle.Math.Primes;
using NPOI.HPSF;
using Microsoft.VisualBasic;
using System.Threading;
using NPOI.XSSF.Streaming.Values;
using ActiproSoftware.Windows.Shapes;
using Org.BouncyCastle.Tsp;
using SharpCompress.Common;
using static NPOI.POIFS.Crypt.CryptoFunctions;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using QueryFilter = ArcGIS.Core.Data.QueryFilter;
using CCTool.Scripts.UI.ProMapTool;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
using CCTool.Scripts.ToolManagers.Library;
using System.Windows.Controls;
using ListBox = System.Windows.Controls.ListBox;
using CheckBox = System.Windows.Controls.CheckBox;
using System.Runtime.Intrinsics.Arm;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Style = Aspose.Cells.Style;
using Aspose.Words.Tables;
using Cell = Aspose.Cells.Cell;
using ArcGIS.Desktop.Internal.Mapping.Raster.RasterHistogram;
using NPOI.POIFS.Crypt;
using static NPOI.HSSF.Util.HSSFColor;
using System.Xml.Linq;


namespace CCTool.Scripts.UI.ProButton
{
    internal class TestButton : Button
    {

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "测试";

        // 标记

        protected override async void OnClick()
        {

            // 获取参数
            string def_folder = Project.Current.HomeFolderPath;     // 工程默认文件夹位置
            string def_gdb = Project.Current.DefaultGeodatabasePath;    // 工程默认数据库

            string folder = @"C:\Users\Administrator\Desktop\朔望\测试";
            string folder2 = @"C:\Users\Administrator\Desktop\朔望\测试2";

            // 获取所有shp文件
            List<string> shps = DirTool.GetAllFiles(folder, ".shp");

            try
            {
                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                await QueuedTask.Run(() =>
                {
                    foreach (var shp in shps)
                    {
                        MapCtlTool.AddLayerToMap(shp);
                        
                        
                        string name = shp[(shp.LastIndexOf("\\")+1)..];
                        string target_shp = $@"{folder2}{name}.shp";
                        pw.AddMessageMiddle(0, target_shp);
                        

                        Arcpy.CopyFeatures($@"{name}.shp", target_shp);
                    }

                    

                });


                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        public void Test()
        {
            string gdbPath = @"D:\【工作项目】\【总规】\安溪国土空间规划\【镇区划定】\1基础资料\350524安溪县(2023).gdb\data";
            string tPath = @"C:\Users\Administrator\Desktop\导出\镇区划定数据";

            string xzq = $@"{gdbPath}\XZQ";
            string dltb = $@"{gdbPath}\DLTB";
            string cjdcq = $@"{gdbPath}\CJDCQ";
            string czcdyd = $@"{gdbPath}\CZCDYD";

            var zqList = GisTool.GetDictFromPath(xzq, "XZQDM", "XZQMC");

            foreach (var zq in zqList)
            {
                string targetPath = @$"{tPath}\{zq.Key}{zq.Value}\2023";
                Directory.CreateDirectory(targetPath);

                Arcpy.CreateFileGDB(targetPath, $"{zq.Key}{zq.Value}");
                string targetGDB = $@"{targetPath}\{zq.Key}{zq.Value}.gdb";
                // 提取
                Arcpy.Select(dltb, $@"{targetGDB}\地类图斑", $"ZLDWDM LIKE '{zq.Key}%'");
                Arcpy.Select(xzq, $@"{targetGDB}\镇级行政区", $"XZQDM = '{zq.Key}'");
                Arcpy.Select(cjdcq, $@"{targetGDB}\村级调查区", $"ZLDWDM LIKE '{zq.Key}%'");
                Arcpy.Select(czcdyd, $@"{targetGDB}\城镇村用地", $"CZCDM LIKE '{zq.Key}%'");
            }
        }

        public void Test1()
        {
            string gdbPath = @"D:\【工作项目】\【总规】\安溪国土空间规划\【镇区划定】\1基础资料\350524安溪县(2021).gdb\data";
            string tPath = @"C:\Users\Administrator\Desktop\导出\镇区划定数据";

            string excelPath = @"C:\Users\Administrator\Desktop\村级行政区.xls";

            string xzq = $@"{gdbPath}\XZQ";
            string dltb = $@"{gdbPath}\DLTB";
            string cjdcq = $@"{gdbPath}\CJDCQ";
            string czcdyd = $@"{gdbPath}\CZCDYD";

            var zqList = GisTool.GetDictFromPath(xzq, "XZQDM", "XZQMC");

            foreach (var zq in zqList)
            {
                string targetExcel = @$"{tPath}\{zq.Key}{zq.Value}\基础设施表.xls";
                File.Copy(excelPath, targetExcel);

                string targetPath = @$"{tPath}\{zq.Key}{zq.Value}\2021";
                string targetGDB = $@"{targetPath}\{zq.Key}{zq.Value}.gdb";

                string cjdcq_2 = $@"{targetGDB}\村级调查区";

                // 提取
                List<string> names = GisTool.GetFieldValuesFromPath(cjdcq_2, "ZLDWMC");

                // 获取工作薄、工作表
                string excelFile = ExcelTool.GetPath(targetExcel);
                int sheetIndex = ExcelTool.GetSheetIndex(targetExcel);
                // 打开工作薄
                Workbook wb = ExcelTool.OpenWorkbook(excelFile);
                // 打开工作表
                Worksheet sheet = wb.Worksheets[sheetIndex];
                // 逐行处理
                for (int i = 0; i < names.Count; i++)
                {
                    if (i > 0)
                    {
                        sheet.Cells.CopyRow(sheet.Cells, 1, i + 1);
                    }

                    sheet.Cells[i + 1, 0].Value = i + 1;
                    sheet.Cells[i + 1, 1].Value = names[i];
                }
                // 保存
                wb.Save(excelFile);
                wb.Dispose();


            }
        }

    }
}
